<template>
    <div class="pageContainer">
        <el-container class="bg-white rounded-lg">
            <el-header style="border-bottom: 1px solid #eeeeee; height: 110px;">
                <div>
                    <el-form :model="searchform" label-width="80px" class="mb-3 flex justify-between items-center pt-5">
                        <el-form-item label="关键词" class="mb-0">
                            <el-input v-model="searchform.keyword" clearable></el-input>
                        </el-form-item>
                        <el-form-item class="mb-0">
                            <el-button type="primary" @click="onSearch">搜索</el-button>
                            <el-button @click="onClearSearch">重置</el-button>
                        </el-form-item>
                    </el-form>
                </div>
                <div class="flex items-center justify-between">
                    <el-button type="primary" @click="onAdd">新增</el-button>
                    <el-button type="primary" text @click="onSearch">
                        <el-icon :size="20">
                            <Refresh />
                        </el-icon>
                    </el-button>
                </div>
            </el-header>
            <el-container>
                <div class="w-full">
                    <el-table :data="tableData" style="width: 100%" :row-class-name="tableRowClassName">
                        <el-table-column label="序号" width="60" align="center" fixed="left" prop="rank">
                            <template #default="{ $index, row }">
                                {{ (currentPage - 1) * limit + $index + 1 }}
                            </template>
                        </el-table-column>
                        <el-table-column prop="name" label="分类名称" min-width="150" />
                        <el-table-column prop="order" label="排序" min-width="100" />
                        <el-table-column prop="status" label="状态" min-width="100">
                            <template #default="{ $index, row }">
                                <el-switch v-model="row.status" :active-value="1" :inactive-value="0" size="small" :lazy="true"
                                    @change="onStatusChange(row)" />
                            </template>
                        </el-table-column>
                        <el-table-column prop="create_time" label="创建时间" min-width="180" />
                        <el-table-column prop="update_time" label="更新时间" min-width="180" />
                        <el-table-column fixed="right" label="操作" min-width="120" align="center">
                            <template #default="scope">
                                <el-button link type="primary" @click="onEdit(scope.row)">修改</el-button>
                                <el-popconfirm title="确定删除该分类吗？" @confirm="onDelete(scope.row)">
                                    <template #reference>
                                        <el-button type="primary" link>删除</el-button>
                                    </template>
                                </el-popconfirm>
                            </template>
                        </el-table-column>
                    </el-table>
                    <div style="height: 50px;" class="flex items-center justify-center">
                        <el-pagination layout="prev, pager, next" background :current-page="currentPage"
                            :total="totalCount" :page-size="10" @current-change="onPageChange" />
                    </div>
                </div>
            </el-container>
        </el-container>
        <FormDrawer ref="drawerRef" :title="drawerTitle" @submit="onSubmit">
            <el-form ref="formRef" :model="form" :rules="rules" label-width="100">
                <el-form-item label="分类名称" prop="name">
                    <el-input v-model="form.name" placeholder="请输入分类名称" clearable />
                </el-form-item>
                <el-form-item label="状态" prop="status">
                    <el-switch v-model="form.status" :active-value="1" :inactive-value="0" />
                </el-form-item>
                <el-form-item label="排序" prop="order">
                    <el-input-number v-model="form.order" :min="1" :max="1000" />
                </el-form-item>
            </el-form>
        </FormDrawer>
    </div>
</template>

<script setup>
import { ref, reactive } from 'vue'
import { getCategoryList, addCategory, updateCategoryById, deleteCategoryById, updateCategoryStatusById } from '../../api/category'
import { toast, openFullScreenLoading, closeFullScreenLoading } from '../../composables/util'
import { formatDateTime } from '../../composables/date'
import FormDrawer from '../../components/FormDrawer.vue'
import { useTableSearch, useForm } from '../../composables/common'

// 定义查询表单
const searchform = reactive({
    keyword: "",
})

const { 
    tableRowClassName,
    onSearch,
    getData,
    onClearSearch,
    onPageChange,
    currentPage,
    tableData,
    totalCount,
    limit
} = useTableSearch({
    searchform,
    getTableList: getCategoryList
})

// 表单
const form = reactive({
    id: 0,
    name: '',
    status: 1,
    order: 50
})

// 表单规则
const rules = {
    name: [{ required: true, message: '分类名称不能为空', trigger: 'blur' }],
    status: [{ required: true, message: '请选择状态', trigger: 'change' }],
    order: [{ required: true, message: '请输入排序值', trigger: 'blur' }],
}

const { 
    formRef,
    drawerRef,
    drawerTitle,
    onAdd,
    onEdit,
    onDelete,
    onSubmit,
    onStatusChange
} = useForm({
    title: '商品分类',
    form,
    tableData,
    getData,
    add: addCategory,
    update: updateCategoryById,
    delete: deleteCategoryById,
    updateStatus: updateCategoryStatusById,
    formDataInit: (form) => {
        if(!form.id) {
            form.order = 50
        }
    }
})
</script>
